//用于转发图片的server
var http = require("http");
var server = http.createServer();
var url = require("url");
var querystring = require("querystring");
server.on('request', function(req, res) {
    var urlOption = url.parse(req.url);
    var pathName = urlOption.pathname;
    if(pathName == '/public-pic'){
        var query = querystring.parse(urlOption.query);
        var img = query.img;
        var imgUrl = url.parse(img);
        imgUrl.headers = {
                "Referer": "https://www.zhihu.com"
            };
        var opt={
            hostname:imgUrl.hostname,
            port:'80',
            path:imgUrl.path,
            headers:imgUrl.headers
        }
        var request = http.request(opt);
        request.on('response', function(response) {
            var c = "";
            response.setEncoding('binary');
            response.on('data', function(chunk) {
                c += chunk;
            });
            response.on('end', function() {
                res.writeHead(200, response.headers);
                res.write(c, "binary");
                res.end();
            })
        });
        request.end();
    }
});
server.listen(8088, function() {
    console.log("listen on 8088");
});
